package com.droidlogic.mboxlauncher;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.LauncherActivityInfo;
import android.content.pm.LauncherApps;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Process;
import android.util.ArrayMap;
import android.util.Log;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class AppDataLoader {
    private final static String SHORTCUTPATH = "shortcut";
    List<ArrayMap<String, Object>> appShortCuts = new ArrayList();
    List<ArrayMap<String, Object>> homeShortCuts = new ArrayList();
    /* access modifiers changed from: private */
    public boolean isLoaded = false;
    private String[] list_homeShortcut;
    private String[] list_localShortcut;
    private String[] list_musicShortcut;
    private String[] list_recommendShortcut;
    private String[] list_videoShortcut;
    List<ArrayMap<String, Object>> localShortCuts = new ArrayList();
    private ActivityManager mActivityManager;
    private Context mContext;
    private LauncherApps mLauncherApps;
    /* access modifiers changed from: private */
    public Object mLock;
    List<ArrayMap<String, Object>> musicShortCuts = new ArrayList();
    List<ArrayMap<String, Object>> recommendShorts = new ArrayList();
    private String str_homeShortcut;
    private String str_localShortcut;
    private String str_musicShortcut;
    private String str_recommendShortcut;
    private String str_videoShortcut;
    List<ArrayMap<String, Object>> videoShortCuts = new ArrayList();

    public AppDataLoader(Context context) {
        this.mContext = context;

        this.mLauncherApps = (LauncherApps) this.mContext.getSystemService(Context.LAUNCHER_APPS_SERVICE);
        this.mActivityManager = (ActivityManager) this.mContext.getSystemService(Context.ACTIVITY_SERVICE);
        this.mLock = ((Launcher) this.mContext).getLock();
    }

    public void update() {
        this.isLoaded = false;
        new Thread(new Runnable() {
            public void run() {
                synchronized (AppDataLoader.this.mLock) {
                    String[] unused = AppDataLoader.this.loadCustomApps();
                    AppDataLoader.this.loadShortcutList();
                    boolean unused2 = AppDataLoader.this.isLoaded = true;
                }
            }
        }).start();
    }

    public String[] loadCustomApps() {
        SharedPreferences sp = mContext.getSharedPreferences(SHORTCUTPATH,Context.MODE_PRIVATE);
        str_homeShortcut = getShortcut("Home_Shortcut",sp);
        list_homeShortcut = str_homeShortcut.split(";");

        str_localShortcut = getShortcut("Local_Shortcut",sp);
        list_localShortcut = str_localShortcut.split(";");

        str_musicShortcut = getShortcut("Music_shortcut",sp);
        list_musicShortcut = str_musicShortcut.split(";");

        str_recommendShortcut = getShortcut("Recommend_Shortcut",sp);
        list_recommendShortcut = str_recommendShortcut.split(";");

        str_videoShortcut = getShortcut("Video_Shortcut",sp);
        list_videoShortcut = str_videoShortcut.split(";");
        return null;
    }

    private String getShortcut(String key,SharedPreferences sp){
        String defStr = "";
        switch (key){
            case "Home_Shortcut":
                defStr = "com.mxtech.videoplayer.ad;com.android.music;com.android.calculator2;";
                break;
            case "Local_Shortcut":
                defStr = "com.android.gallery3d;com.droidlogic.videoplayer;com.droidlogic.FileBrower;";
                break;
            case "Music_shortcut":
                defStr = "com.moretv.mv;com.android.music;com.hycstv.android;";
                break;
            case "Recommend_Shortcut":
                defStr = "com.droidlogic.mediacenter;com.droidlogic.miracast;";
                break;
            case "Video_Shortcut":
                defStr = "com.moretv.android;com.qiyi.video;com.togic.livevideo;com.sohutv.tv;com.youku.tv;net.myvst.v2;com.elinkway.tvlive2;";
                break;
        }
        return sp.getString(key,defStr);

    }
    public void saveShortcut(int mode, String strCustomApps) {
        synchronized (mLock){
            SharedPreferences sp = mContext.getSharedPreferences(SHORTCUTPATH,Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sp.edit();
            String shortcutKey = parseShortcutHead(mode);
            editor.putString(shortcutKey,strCustomApps).apply();
        }
    }

    private static final Comparator<LauncherActivityInfo> getAppNameComparator() {
        final Collator collator = Collator.getInstance();
        return new Comparator<LauncherActivityInfo>() {
            public final int compare(LauncherActivityInfo a, LauncherActivityInfo b) {
                if (!a.getUser().equals(b.getUser())) {
                    return a.getUser().toString().compareTo(b.getUser().toString());
                }
                int result = collator.compare(a.getLabel().toString(), b.getLabel().toString());
                if (result == 0) {
                    return a.getName().compareTo(b.getName());
                }
                return result;
            }
        };
    }

    /* access modifiers changed from: private */
    public void loadShortcutList() {
        this.homeShortCuts.clear();
        this.videoShortCuts.clear();
        this.recommendShorts.clear();
        this.musicShortCuts.clear();
        this.appShortCuts.clear();
        this.localShortCuts.clear();
        List<LauncherActivityInfo> apps = this.mLauncherApps.getActivityList((String) null, Process.myUserHandle());
        Collections.sort(apps, getAppNameComparator());
        int iconDpi = this.mActivityManager.getLauncherLargeIconDensity();
        if (apps != null) {
            for (int i = 0; i < apps.size(); i++) {
                LauncherActivityInfo info = apps.get(i);
                if (BuildConfig.APPLICATION_ID.equals(info.getComponentName().getPackageName())){
                    continue;
                }
                ApplicationInfo application = new ApplicationInfo();
                application.title = info.getLabel().toString();
                application.setActivity(info.getComponentName(), 270532608);
                application.icon = info.getBadgedIcon(iconDpi);
                if ("com.famousbluemedia.yokee".equals(application.componentName.getPackageName())){
                    Log.e("Launcher","替换图标在这里改");
                    application.icon = mContext.getDrawable(R.drawable.ic_yokee);
                }

                if (!info.getComponentName().getPackageName().equals("com.android.gallery3d") || !application.intent.toString().contains("camera")) {
                    if (this.list_homeShortcut != null) {
                        int j = 0;
                        while (true) {
                            if (j >= this.list_homeShortcut.length) {
                                break;
                            } else if (info.getComponentName().getPackageName().equals(this.list_homeShortcut[j])) {
                                this.homeShortCuts.add(buildShortcutMap(application.title.toString(), application.intent, application.icon, application.componentName));
                                break;
                            } else {
                                j++;
                            }
                        }
                    }
                    if (this.list_videoShortcut != null) {
                        int j2 = 0;
                        while (true) {
                            if (j2 >= this.list_videoShortcut.length) {
                                break;
                            } else if (info.getComponentName().getPackageName().equals(this.list_videoShortcut[j2])) {
                                this.videoShortCuts.add(buildShortcutMap(application.title.toString(), application.intent, application.icon, application.componentName));
                                break;
                            } else {
                                j2++;
                            }
                        }
                    }
                    if (this.list_recommendShortcut != null) {
                        int j3 = 0;
                        while (true) {
                            if (j3 >= this.list_recommendShortcut.length) {
                                break;
                            } else if (info.getComponentName().getPackageName().equals(this.list_recommendShortcut[j3])) {
                                this.recommendShorts.add(buildShortcutMap(application.title.toString(), application.intent, application.icon, application.componentName));
                                break;
                            } else {
                                j3++;
                            }
                        }
                    }
                    if (this.list_musicShortcut != null) {
                        int j4 = 0;
                        while (true) {
                            if (j4 >= this.list_musicShortcut.length) {
                                break;
                            } else if (info.getComponentName().getPackageName().equals(this.list_musicShortcut[j4])) {
                                this.musicShortCuts.add(buildShortcutMap(application.title.toString(), application.intent, application.icon, application.componentName));
                                break;
                            } else {
                                j4++;
                            }
                        }
                    }
                    if (this.list_localShortcut != null) {
                        int j5 = 0;
                        while (true) {
                            if (j5 >= this.list_localShortcut.length) {
                                break;
                            } else if (info.getComponentName().getPackageName().equals(this.list_localShortcut[j5])) {
                                this.localShortCuts.add(buildShortcutMap(application.title.toString(), application.intent, application.icon, application.componentName));
                                break;
                            } else {
                                j5++;
                            }
                        }
                    }
                    this.appShortCuts.add(buildShortcutMap(application.title.toString(), application.intent, application.icon, application.componentName));
                    application.icon.setCallback((Drawable.Callback) null);
                }
            }
        }
        this.homeShortCuts.add(buildAddMap());
        this.videoShortCuts.add(buildAddMap());
        this.musicShortCuts.add(buildAddMap());
        this.localShortCuts.add(buildAddMap());
        this.recommendShorts.add(buildAddMap());
    }

    private ArrayMap<String, Object> buildShortcutMap(String name, Intent i, Drawable icon, ComponentName c) {
        ArrayMap<String, Object> map = new ArrayMap<>();
        map.put("name", name);
        map.put("intent", i);
        int resId = parsePackageIcon(c.getPackageName());
        if (resId == -1) {
            map.put("icon", icon);
        } else {
            map.put("icon", this.mContext.getResources().getDrawable(resId));
        }
        map.put("component name", c);
        return map;
    }

    private ArrayMap<String, Object> buildAddMap() {
        ArrayMap<String, Object> map = new ArrayMap<>();
        map.put("name", this.mContext.getResources().getString(R.string.str_add));
        map.put("intent", (Object) null);
        map.put("icon", Integer.valueOf(R.drawable.item_img_add));
        return map;
    }

    public List<ArrayMap<String, Object>> getShortcutList(int mode) {
        Log.e("Launcher","getShortcutList>>"+mode);
        synchronized (this.mLock) {
            switch (mode) {
                case 0:
                    List<ArrayMap<String, Object>> list = this.homeShortCuts;
                    return list;
                case 1:
                    List<ArrayMap<String, Object>> list2 = this.videoShortCuts;
                    return list2;
                case 2:
                    List<ArrayMap<String, Object>> list3 = this.recommendShorts;
                    return list3;
                case 3:
                    List<ArrayMap<String, Object>> list4 = this.musicShortCuts;
                    return list4;
                case 4:
                    List<ArrayMap<String, Object>> list5 = this.appShortCuts;
                    return list5;
                case 5:
                    List<ArrayMap<String, Object>> list6 = this.localShortCuts;
                    return list6;
                default:
                    return null;
            }
        }
    }

    public String getShortcutString(int mode) {
        synchronized (this.mLock) {
            switch (mode) {
                case 0:
                    String str = this.str_homeShortcut;
                    return str;
                case 1:
                    String str2 = this.str_videoShortcut;
                    return str2;
                case 2:
                    String str3 = this.str_recommendShortcut;
                    return str3;
                case 3:
                    String str4 = this.str_musicShortcut;
                    return str4;
                case 5:
                    String str5 = this.str_localShortcut;
                    return str5;
                default:
                    return null;
            }
        }
    }

    public boolean isDataLoaded() {
        return this.isLoaded;
    }

    private String parseShortcutHead(int mode) {
        switch (mode) {
            case 0:
                return "Home_Shortcut";
            case 1:
                return "Video_Shortcut";
            case 2:
                return "Recommend_Shortcut";
            case 3:
                return "Music_shortcut";
            case 5:
                return "Local_Shortcut";
            default:
                return null;
        }
    }

    private int parsePackageIcon(String packageName) {
        if (packageName.equals("com.droidlogic.FileBrower")) {
            return R.drawable.icon_filebrowser;
        }
        if (packageName.equals("com.android.browser")) {
            return R.drawable.icon_browser;
        }
        if (packageName.equals("com.droidlogic.appinstall")) {
            return R.drawable.icon_appinstaller;
        }
        if (packageName.equals("com.android.tv.settings")) {
            return R.drawable.icon_setting;
        }
        if (packageName.equals("com.droidlogic.mediacenter")) {
            return R.drawable.icon_mediacenter;
        }
        if (packageName.equals("com.droidlogic.otaupgrade")) {
            return R.drawable.icon_backupandupgrade;
        }
        if (packageName.equals("com.android.gallery3d")) {
            return R.drawable.icon_pictureplayer;
        }
        if (packageName.equals("com.droidlogic.miracast")) {
            return R.drawable.icon_miracast;
        }
        if (packageName.equals("com.droidlogic.PPPoE")) {
            return R.drawable.icon_pppoe;
        }
        if (packageName.equals("com.android.music")) {
            return R.drawable.icon_music;
        }
        if (packageName.equals("com.android.camera2")) {
            return R.drawable.icon_camera;
        }
        return -1;
    }
}